package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.config.CommonConfig;
import com.zhijian.medical.entity.dto.*;
import com.zhijian.medical.entity.vo.request.HstMeetingV2SaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.HstMeetingV2SearchVo;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.HstMeetingV2Service;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Date3Util;
import com.zhijian.medical.util.HstMeetingV2Util;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * @Description: TODO
 * @Author: hcg
 * @Date: 2024/11/20
 * @Version: 1.0
 */
@Service
@Slf4j
public class HstMeetingV2ServiceImpl implements HstMeetingV2Service {

    @Autowired
    private CommonConfig commonConfig;

    @Override
    public boolean createMeeting(HstMeetingV2SaveOrUpdateVo saveOrUpdateVo) {

//        String password = saveOrUpdateVo.getPassword();
//        if (StringUtils.isNotBlank(password)) {
//            //设置了密码，模式就是会议室密码验证
//            saveOrUpdateVo.setVerifyMode(2 + "");
//        } else {
//            //否则就是账户密码登录
//            saveOrUpdateVo.setVerifyMode(1 + "");
//        }

        //默认匿名登录
        saveOrUpdateVo.setVerifyMode(3 + "");
        saveOrUpdateVo.setMeetingMinutes(true);
        saveOrUpdateVo.setTerminalReInvite(true);

        HstMeetingV2CreateRequestDto requestDto = new HstMeetingV2CreateRequestDto();
        BeanUtils.copyProperties(saveOrUpdateVo, requestDto);

        if (StringUtils.isBlank(saveOrUpdateVo.getPassword())) {
            requestDto.setPassword(null);
        }

        //签名
        String sign = HstMeetingV2Util.getSign(commonConfig.getMeetingHstV2Secret(), commonConfig.getMeetingHstV2Key());

        HstMeetingV2CreateResponseDto responseDto = HstMeetingV2Util.createMeeting(sign, requestDto);
        AssertUtil.check(Objects.isNull(responseDto) || !"0".equals(responseDto.getCode()), "创建失败，请联系管理员");
        //返回的会议ID
        Integer roomId = responseDto.getData().getRoomId();
        //还要调用修改会议类型为预约会议的接口
        HstMeetingV2ReservationRequestDto reservationRequestDto = new HstMeetingV2ReservationRequestDto();
        reservationRequestDto.setRoomId(roomId);
        reservationRequestDto.setHopeStartTime(saveOrUpdateVo.getHopeStartTime());
        reservationRequestDto.setHopeEndTime(saveOrUpdateVo.getHopeEndTime());
        reservationRequestDto.setScheduleAdvance(true);
        reservationRequestDto.setExtension(false);
        HstMeetingV2CommonResult reservation = HstMeetingV2Util.reservation(sign, reservationRequestDto);
        return Objects.nonNull(reservation) && "0".equals(responseDto.getCode());
    }

    @Override
    public Page<HstMeetingV2MeetingResponseDto.PageV2Bean.DataV2Bean> searchMeeting(HstMeetingV2SearchVo searchVo) {

        //签名
        String sign = HstMeetingV2Util.getSign(commonConfig.getMeetingHstV2Secret(), commonConfig.getMeetingHstV2Key());

        HstMeetingV2SearchMeetingDto searchMeetingDto = new HstMeetingV2SearchMeetingDto();
        searchMeetingDto.setPageNumber(searchVo.getPageNum());
        searchMeetingDto.setPageSize(searchVo.getPageSize());
        searchMeetingDto.setSearchKey(searchVo.getMeetingName());

        HstMeetingV2MeetingResponseDto hstMeetingV2MeetingResponseDto = HstMeetingV2Util.roomList(sign, searchMeetingDto);
        if (Objects.nonNull(hstMeetingV2MeetingResponseDto) && "0".equals(hstMeetingV2MeetingResponseDto.getCode())) {
            HstMeetingV2MeetingResponseDto.PageV2Bean pageV2Bean = hstMeetingV2MeetingResponseDto.getData();
            if (Objects.nonNull(pageV2Bean)) {
                Page<HstMeetingV2MeetingResponseDto.PageV2Bean.DataV2Bean> page = new Page<>();
                page.setTotalCount(pageV2Bean.getTotal());

                //如果有值，增加个开启会议的URL
                List<HstMeetingV2MeetingResponseDto.PageV2Bean.DataV2Bean> records = pageV2Bean.getRecords();
                if (CollectionUtil.isNotEmpty(records)) {

                    for (HstMeetingV2MeetingResponseDto.PageV2Bean.DataV2Bean record : records) {
                        record.setStartMeetingUrl(String.format(commonConfig.getStartMeetingUrl(), CurrentUserHolder.getUser().getName(), record.getRoomId()));
                        String hopeStartTime = record.getHopeStartTime();
                        if (StringUtils.isNotBlank(hopeStartTime)) {
                            record.setHopeStartTime(Date3Util.transformDate(hopeStartTime));
                        }
                        String hopeEndTime = record.getHopeEndTime();
                        if (StringUtils.isNotBlank(hopeEndTime)) {
                            record.setHopeEndTime(Date3Util.transformDate(hopeEndTime));
                        }
                        String startTime = record.getStartTime();
                        if (StringUtils.isNotBlank(startTime)) {
                            record.setStartTime(Date3Util.transformDate(startTime));
                        }
                        String endTime = record.getEndTime();
                        if (StringUtils.isNotBlank(endTime)) {
                            record.setEndTime(Date3Util.transformDate(endTime));
                        }
                    }

                    page.setData(records);
                }

                page.setPageNum(pageV2Bean.getCurrent());
                page.setPageSize(pageV2Bean.getSize());
                page.setPage(true);
                page.setPageCount(pageV2Bean.getPages());
                return page;
            }
        }
        return new Page<>();
    }

    public static void main(String[] args) {
        // 原始时间字符串
        String originalDateTimeStr = "2024-11-08T14:22:39.000+08:00";

        // 定义原始字符串的格式
        DateTimeFormatter originalFormatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

        // 解析原始字符串为OffsetDateTime对象
        OffsetDateTime offsetDateTime = OffsetDateTime.parse(originalDateTimeStr, originalFormatter);

        // 定义目标字符串的格式
        DateTimeFormatter targetFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 格式化OffsetDateTime对象为目标字符串
        String formattedDateTimeStr = offsetDateTime.format(targetFormatter);

        // 输出结果
        System.out.println(formattedDateTimeStr);
    }
}
